<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *提现管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'申请提现列表'),
	array('op'=>'ok','title'=>'审核通过列表'),	
	array('op'=>'no','title'=>'审核不通过列表'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=0;
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','money','overtime','status','createtime');
	$list=pdo_getslice('jy_quntui_cashrecord',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_quntui_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$item['nickname']=$user['nickname'];
			$item['avatar']=$user['avatar'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'nickname','title'=>'申请人','type'=>'string'),
			array('name'=>'avatar','title'=>'申请人头像','type'=>'image'),
			array('name'=>'money','title'=>'申请提现金额','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
		),
		 'action' => 
		    array (
			       array('type'=>'post','op'=>'pass','title'=>'审核通过','message'=>'确定通过该申请吗，通过后提现金额将发送到用户钱包！'),
			       array('type'=>'post','op'=>'unpass','title'=>'审核不通过','message'=>'确定拒绝该申请吗！'),
				   ),
				   ),
		'search'=>array(
			array('name'=>'mid','width'=>'15','title'=>'会员id','type'=>'string'),
			array('name'=>'nickname','width'=>'15','title'=>'昵称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='ok'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=1;
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','money','overtime','status','createtime');
	$list=pdo_getslice('jy_quntui_cashrecord',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_quntui_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$item['nickname']=$user['nickname'];
			$item['avatar']=$user['avatar'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'nickname','title'=>'申请人','type'=>'string'),
			array('name'=>'avatar','title'=>'申请人头像','type'=>'image'),
			array('name'=>'money','title'=>'申请提现金额','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
			array('name'=>'overtime','title'=>'通过时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'mid','width'=>'15','title'=>'会员id','type'=>'string'),
			array('name'=>'nickname','width'=>'15','title'=>'昵称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='no'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=2;
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','money','overtime','status','createtime');
	$list=pdo_getslice('jy_quntui_cashrecord',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_quntui_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$item['nickname']=$user['nickname'];
			$item['avatar']=$user['avatar'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'nickname','title'=>'申请人','type'=>'string'),
			array('name'=>'avatar','title'=>'申请人头像','type'=>'image'),
			array('name'=>'money','title'=>'申请提现金额','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
			array('name'=>'overtime','title'=>'拒绝时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'mid','width'=>'15','title'=>'会员id','type'=>'string'),
			array('name'=>'nickname','width'=>'15','title'=>'昵称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_quntui_cashrecord',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}

if($_RQ['op']=='unpass'){
	   $id=intval($_RQ['id']);
	   if(!$id){
		   exi('该申请不存在，请重新选择！','error');
	   }
	   $cashrecord=pdo_get('jy_quntui_cashrecord',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	   $user=pdo_get('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$cashrecord['mid']));
		pdo_update('jy_quntui_cashrecord',array('status'=>2,'overtime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		//将申请提现金额返回到可提现金额中
		pdo_update('jy_quntui_user',array('cash'=>$user['cash']+$cashrecord['money']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$user['mid']));
	   exi('拒绝申请成功','success');
}
  
if($_RQ['op']=='pass'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('该申请不存在，请重新选择！','error');
	}
	$cashrecord=pdo_get('jy_quntui_cashrecord',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$user=pdo_get('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$cashrecord['mid']));
	$fans=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$cashrecord['mid']));
	$basic=$this->plugin_setting('basic');
	$arr['openid'] = $fans['openid'];
	$arr['hbname'] = '场景投票';
	$arr['body'] = "场景投票红包";
	$arr['fee'] = $cashrecord['money'];
	$arr['mchid'] = $basic['mchid'];
	$arr['mch_appid'] = $basic['appid'];
	$arr['username'] = $user['nickname']?$user['nickname']:$fans['nickname'];
	$res = sendhongbaoto($arr);
	if($res['result_code'] == 'SUCCESS'){
		pdo_update('jy_quntui_cashrecord',array('status'=>1,'overtime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('提现成功，金额已转入到对方钱包！');
		
	}else{
		$res['result_msg']=$res['result_msg']?$res['result_msg']:'检查微信商户是否已设置,商户金额等';
		exi('提现失败：'.$res['result_msg'],'error');
	}
	
}

		
